package com.maidou.qiantai.controller;

import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.maidou.qiantai.dto.member.CartDeleteDto;
import com.maidou.qiantai.dto.member.CartDto;
import com.maidou.qiantai.vo.member.CartVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.maidou.qiantai.domain.Cart;
import com.maidou.qiantai.service.ICartService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 购物车Controller
 * 
 * @author maidou
 * @date 2024-03-13
 */
@RestController
@RequestMapping("/qiantai/cart")
public class CartController extends BaseController
{
    @Autowired
    private ICartService cartService;

    /**
     * 查询购物车列表
     */
    @GetMapping("/list")
    public TableDataInfo list()
    {
        startPage();
        List<CartVo> list = cartService.selectCartVoList();
        return getDataTable(list);
    }
    /**
     * 查询购物车列表
     */
    @PostMapping("/order-list")
    public AjaxResult listOrder(@RequestBody HashMap<String,Object> map)
    {
//        startPage();
        List<CartVo> list = cartService.selectCartVoOrderList(map);
        return AjaxResult.success(list);
    }

    /**
     * 新增购物车
     */
    @PostMapping("/add-cart")
    public AjaxResult add(@RequestBody CartDto cart)
    {
        return toAjax(cartService.insertDtoCart(cart));
    }

    /**
     * 新增购物车
     */
    @GetMapping("/cart-num/{id}/{num}")
    public AjaxResult update(@PathVariable("id") Long id,@PathVariable("num") Integer num)
    {
        return toAjax(cartService.updateCartNum(id,num));
    }

    /**
     * 删除购物车
     */
    @PostMapping("/cart-delete")
    public AjaxResult deleteCart(@RequestBody HashMap<String,Object> map)
    {
//        List<String> ids = cartDeleteDto.getIds();
        CartDeleteDto cartDeleteDto = JSONObject.parseObject(JSON.toJSONString(map), CartDeleteDto.class);
        Long[] longs = JSONObject.parseObject(JSON.toJSONString(cartDeleteDto.getIds()), Long[].class);

        return toAjax(cartService.deleteCartByIds(longs));
    }


    /**
     * 导出购物车列表
     */
    @PreAuthorize("@ss.hasPermi('qiantai:cart:export')")
    @Log(title = "购物车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Cart cart)
    {
        List<Cart> list = cartService.selectCartList(cart);
        ExcelUtil<Cart> util = new ExcelUtil<Cart>(Cart.class);
        util.exportExcel(response, list, "购物车数据");
    }

    /**
     * 获取购物车详细信息
     */
    @PreAuthorize("@ss.hasPermi('qiantai:cart:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cartService.selectCartById(id));
    }

    /**
     * 新增购物车
     */
    @PreAuthorize("@ss.hasPermi('qiantai:cart:add')")
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Cart cart)
    {
        return toAjax(cartService.insertCart(cart));
    }

    /**
     * 修改购物车
     */
    @PreAuthorize("@ss.hasPermi('qiantai:cart:edit')")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Cart cart)
    {
        return toAjax(cartService.updateCart(cart));
    }

    /**
     * 删除购物车
     */
    @PreAuthorize("@ss.hasPermi('qiantai:cart:remove')")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cartService.deleteCartByIds(ids));
    }
}
